package com.osce.score.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csce.domain.entity.score.ScoreExExamPlaceAllot;
import com.csce.domain.entity.score.ScoreExExamSession;
import com.doudou.core.container.ListUtil;
import com.osce.interfaces.score.IScoreExExamPlaceAllotService;
import com.osce.score.mapper.ScoreExExamPlaceAllotMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 考试分配学员Service业务层处理
 *
 * @author zzx
 * @since 2023-09-07
 */
@Service
public class ScoreExExamPlaceAllotServiceImpl extends ServiceImpl<ScoreExExamPlaceAllotMapper, ScoreExExamPlaceAllot> implements IScoreExExamPlaceAllotService {


    /**
     * 查询考试分配学员
     *
     * @param id 考试分配学员主键
     * @return 考试分配学员
     */
    @Override
    public ScoreExExamPlaceAllot selectScoreExExamPlaceAllotById(Long id) {
        return baseMapper.selectScoreExExamPlaceAllotById(id);
    }

    /**
     * 查询考试分配学员列表
     *
     * @param scoreExExamPlaceAllot 考试分配学员
     * @return 考试分配学员
     */
    @Override
    public List<ScoreExExamPlaceAllot> selectScoreExExamPlaceAllotList(ScoreExExamPlaceAllot scoreExExamPlaceAllot) {
        return baseMapper.selectScoreExExamPlaceAllotList(scoreExExamPlaceAllot);
    }

    /**
     * 新增考试分配学员
     *
     * @param scoreExExamPlaceAllot 考试分配学员
     * @return 结果
     */
    @Override
    public int insertScoreExExamPlaceAllot(ScoreExExamPlaceAllot scoreExExamPlaceAllot) {
        return baseMapper.insertScoreExExamPlaceAllot(scoreExExamPlaceAllot);
    }

    /**
     * 修改考试分配学员
     *
     * @param scoreExExamPlaceAllot 考试分配学员
     * @return 结果
     */
    @Override
    public int updateScoreExExamPlaceAllot(ScoreExExamPlaceAllot scoreExExamPlaceAllot) {
        return baseMapper.updateScoreExExamPlaceAllot(scoreExExamPlaceAllot);
    }

    /**
     * 批量删除考试分配学员
     *
     * @param ids 需要删除的考试分配学员主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamPlaceAllotByIds(Long[] ids) {
        return baseMapper.deleteScoreExExamPlaceAllotByIds(ids);
    }

    /**
     * 删除考试分配学员信息
     *
     * @param id 考试分配学员主键
     * @return 结果
     */
    @Override
    public int deleteScoreExExamPlaceAllotById(Long id) {
        return baseMapper.deleteScoreExExamPlaceAllotById(id);
    }


    /**
     * 新增考试分配学员信息
     */
    @Override
    public int insertScoreExExamPlaceAllot(List<ScoreExExamPlaceAllot> scoreExExamPlaceAllotList, Long sessionId) {
        if (ListUtil.isEmpty(scoreExExamPlaceAllotList)) {
            return 0;
        }
        scoreExExamPlaceAllotList = scoreExExamPlaceAllotList.stream().peek(allot -> allot.setSessionId(sessionId)).collect(Collectors.toList());
        return baseMapper.batchScoreExExamPlaceAllot(scoreExExamPlaceAllotList);
    }

    @Override
    public int deleteScoreExExamPlaceAllotBySessionId(Long id) {
        return baseMapper.deleteScoreExExamPlaceAllotBySessionId(id);
    }

    @Override
    public int deleteScoreExExamPlaceAllotBySessionIds(Long[] ids) {
        return baseMapper.deleteScoreExExamPlaceAllotBySessionIds(ids);
    }

    @Override
    public int insertScoreExExamPlaceAllotBySession(List<ScoreExExamSession> sessions) {
        return baseMapper.insertScoreExExamPlaceAllotBySession(sessions);
    }

    @Override
    public int clearUserIdsByExam(Long examId) {
        return baseMapper.updateUserIdNullByExam(examId);
    }

    @Override
    public List<String> selectCodeByExamIdGroupAndOrderByCode(Long id) {
        return baseMapper.selectCodeByExamIdGroupAndOrderByCode(id);
    }

    @Override
    public int updateUserIdByExamIdAndCode(Long id, Long userId, String code) {
        return baseMapper.updateUserIdByCode(id, code, userId);
    }

    /**
     * 获取已安排学员的个数
     */
    @Override
    public int planUserCountByExamId(Long examId) {
        return baseMapper.planUserCountByExamId(examId);
    }

    @Override
    public ScoreExExamPlaceAllot selectScoreExExamPlaceAllotBySession(String examId, String timeId, String placeId, String code) {
        return baseMapper.selectScoreExExamPlaceAllotBySession(examId, timeId, placeId, code);
    }
}
